package com.iwuke.dance.wxapi;

/**
 * @author: 魏从响
 * @date: 2025/9/18
 */
// 路径必须为: src/main/java/您的包名/wxapi/WXPayEntryActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.iwuke.dance.activity.LPWHApplication;
import com.iwuke.dance.util.LogUtil;
import com.iwuke.dance.util.ToastUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private static final String TAG = "WeChatPay";
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, LPWHApplication.app_id);
        api.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        // 处理微信请求
        Log.d(TAG, "onReq, reqType = " + baseReq.getType());
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:  // 支付成功
                    LogUtil.logD(TAG, "支付成功");
                    ToastUtil.setToast(WXPayEntryActivity.this,"支付成功11");
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:  // 用户取消
                    LogUtil.logD(TAG, "用户取消支付");
                    ToastUtil.setToast(WXPayEntryActivity.this,"用户取消支付11");
                    break;
                case BaseResp.ErrCode.ERR_COMM:  // 通信错误
                    Log.e(TAG, "通信错误：" + resp.errStr);
                    ToastUtil.setToast(WXPayEntryActivity.this,"通信错误11：" + resp.errStr);
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:  // 认证失败
                    Log.e(TAG, "支付认证失败");
                    ToastUtil.setToast(WXPayEntryActivity.this,"支付认证失败11");
                    break;
                default:  // 其他错误
                    Log.e(TAG, "支付失败：" + resp.errCode);
                    ToastUtil.setToast(WXPayEntryActivity.this,"支付失败11：" + resp.errCode);
                    break;
            }
        }
        finish();  // 关闭当前Activity
    }
}
